<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        li{
            list-style: none;
        }
        .content{
            width: 400px;
            height: 400px;
            border: 1px solid #000;
            margin: 20px auto;
            position: relative;
            overflow: hidden;
           
        }
        .content .ul{
            display: flex;
            transition:all 0.5s;
            width:2950px;
        }
        .content .ul img{
            display: block;
            width: 400px;
            height: 400px;
        }
        .btn-box{
            width: 400px;
            height: 30px;
            /* background: plum; */
            position: absolute;
            top:200px;
        }
        .btn-box div{
            width: 50px;
            height: 50px;
            background-color:rgba(0,0,0,.5);
            text-align: center;
            line-height: 50px;
            font-size: 28px;
            color: #fff;
        }
        .btn-box .right-btn{
            position:absolute;
            right:0;
            top:0;
        }
        .ctrl-box{
            width: 150px;
            height: 20px;
            background: rgba(0,0,0,.5);
            position: absolute;
            bottom: 20px;
            left: 120px;
            display: flex;
            justify-content: space-between;
        }
        .pointer{
            width: 16px;
            height: 16px;
            border-radius: 50%;
            border: 1px solid #fff;
        }
        .active{
            background: #fff;
        }
    </style>
</head>
<body>
    <div class="content">
        <ul class="ul">
            <li><img src="../img/2.jpeg"></li>
            <li><img src="../img/3.jpg"></li>
            <li><img src="../img/a.jpg"></li>
            <li><img src="../img/c.jpg"></li>
            <li><img src="../img/d.jpg"></li>


        </ul>
        <div class="btn-box">
                <div class="left-btn">&lt;</div>
                <div class="right-btn">&gt;</div>
            </div>
            <div class="ctrl-box">
                <div class="pointer active"></div>
                <div class="pointer"></div>
                <div class="pointer"></div>
                <div class="pointer"></div>
                <div class="pointer"></div>
            </div>
    </div>
    <script>
        function $(ele){
            return document.querySelector(ele)
        }
        let time=null;
        let index=0;
        let img_width=$('img').offsetWidth;
        let pointer=$('.ctrl-box').getElementsByClassName('pointer')
            //console.log(img_width);
        function fun(ope){
           
            if(ope=='+'){
                if(index>=4){
                    index=0;
                }else{
                    index++;
                }
            }else  if(ope=='-'){
                if(index<=0){
                    index=4;
                }else{
                    index--;
                }
            }
            
            $('.ul').style.marginLeft=`${-img_width*index}px`;
            for(let i=0;i<pointer.length;i++){
                pointer[i].className='pointer';
            }
            pointer[index].className='pointer active';
        }
        time=setInterval(function(){
            fun('+');

        },2000)
        $('.content').onmouseenter=function(){
            clearInterval(time);
        }
        $('.content').onmouseleave=function(){
                time=setInterval(function(){
                fun('+');

            },2000)
        }
        
        $('.left-btn').onclick=function(){
            fun('-');
        }
        $('.right-btn').onclick=function(){
            fun('+');
        }
        for(var i=0;i<pointer.length;i++){
            pointer[i].index=i;
        }
        $('.ctrl-box').addEventListener('click',function(e){
            //console.log(e.target);
            if(e.target.className=='pointer'){
                for(let j=0;j<pointer.length;j++){
                   pointer[j].className='pointer'
                }
                e.target.className='pointer active';
                index=e.target.index;
                $('.ul').style.marginLeft=`${-img_width*index}px`;

            }
            
        },false)
     
    </script>
</body>
</html>